package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgConsumptionDataMapper;
import com.csun.dsms.mapper.HgGdpDataMapper;
import com.csun.dsms.mapper.HgUserMapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.HgConsumptionDataService;
import com.csun.dsms.service.HgGdpDataService;
import com.csun.dsms.service.HgUserService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgConsumptionDataServiceImpl extends BaseService<HgConsumptionData> implements HgConsumptionDataService {

    @Resource
    private HgConsumptionDataMapper hgConsumptionDataMapper;

    @Override
    public List<HgConsumptionData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgConsumptionDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public List<HgYearMonthVo> groupByMonth(HgQueryDto hgQueryDto) {
        return hgConsumptionDataMapper.groupByMonth(hgQueryDto);
    }

    @Override
    public Wrapper importConsumptionData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgConsumptionData hgConsumptionData = new HgConsumptionData();
                hgConsumptionData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgConsumptionData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgConsumptionData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgConsumptionData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgConsumptionData.setTotal(Convert.toDouble(excelMap.get("零售总额")));
                hgConsumptionData.setGrowthTotal(Convert.toDouble(excelMap.get("零售总额增加值")));
                hgConsumptionData.setGrowthRate(Convert.toDouble(excelMap.get("零售总额同比增速")));
                hgConsumptionData.setTopRate(Convert.toDouble(excelMap.get("增速增加几个百分点")));
                hgConsumptionData.setGrowthRateRanking(Convert.toInt(excelMap.get("增速排名")));
                hgConsumptionData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgConsumptionData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgConsumptionData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgConsumptionData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgConsumptionData.setCreatedTime(new Date());
                hgConsumptionDataMapper.insert(hgConsumptionData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入社会消费零售总额的数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }
}
